<?php
function getPageURL($page) {
    $uri = $_SERVER['REQUEST_URI'];
    $arr = explode('?', $uri);
    $query = isset($arr[1]) ? $arr[1] : '';
    $query = preg_replace('/[\\?&]?page=[0-9]*/', '', $query);

    if (empty($query)) $query = 'page=' . $page;
    else {
        if ($query[0] == '&') $query = substr($query, 1);
        $query .= '&page=' . $page;
    }

    return '?' . $query;
}
    if ($totalPage <= 1) return "Hello world";
    $fromPage = $page - 2;
    $fromPage = ($fromPage < 1) ? 1 : $fromPage;
    $toPage = $page + 2;
    $toPage = ($toPage > $totalPage) ? $totalPage : $toPage;

    $nextPage = $page + 1;
    $previousPage = $page - 1;
?>
<div class="pagelist"<?php echo isset($noneFloat) ? ' style="float:none"' : ''?>>
    <div class="page">Trang </div>
    <?php if ($page > 1): ?>
        <div class="pageitem"><a href="<?php echo getPageURL(1); ?>"><<</a></div>
    <?php endif;?>

    <?php if ($previousPage > 0): ?>
        <div class="pageitem"><a href="<?php echo getPageURL($previousPage); ?>" title="Previous"><</a></div>
    <?php endif; ?>

    <?php for ($i = $fromPage; $i <= $toPage; $i++): ?>
    <?php if ($i != $page): ?>
        <div class="pageitem"><a href="<?php echo getPageURL($i);?>"><?php echo $i;?></a></div>
    <?php else: ?>
        <div class="pageitem"><a href="javascript:;" class="active"><?php echo $i;?></a></div>
    <?php endif; ?>
    <?php endfor; ?>

    <?php if ($nextPage <= min($toPage, $totalPage + 1)): ?>
    <div class="pageitem"><a href="<?php echo getPageURL($nextPage);?>" title="Next">></a></div>
    <?php endif; ?>

    <?php if ($page < $totalPage): ?>
        <div class="pageitem"><a href="<?php echo getPageURL($totalPage);?>">>></a></div>
    <?php endif;?>
</div>